package com.zrf.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.common.database.utils.Query;
import com.zrf.gulimall.product.dao.ProductAttrValueDao;
import com.zrf.gulimall.product.entity.ProductAttrValueEntity;
import com.zrf.gulimall.product.service.ProductAttrValueService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<ProductAttrValueEntity> queryBySpuId(Long spuId) {
		return this.lambdaQuery()
				.eq(ProductAttrValueEntity::getSpuId, spuId)
				.list();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateAttrValue(Long spuId, List<ProductAttrValueEntity> productAttrValueEntities) {
		this.lambdaUpdate()
				.eq(ProductAttrValueEntity::getSpuId, spuId)
				.remove();
		productAttrValueEntities.forEach(item -> {
			item.setSpuId(spuId);
		});
		this.saveBatch(productAttrValueEntities);
	}

}